﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication3;
namespace bsy
{
    public partial class Card : Form
    {
        private string cart_id="";//卡ID
        public Card()
        {
            InitializeComponent();
            add_combox_type();
        }

        //判断你是否为浮点型
        public Boolean isFloat(string f)
        {
            try
            {
                float i = float.Parse(f);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //判断是否为整型
        public Boolean isInt(string s)
        {
            try
            {
                int i = int.Parse(s);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //显示卡类型
        private void add_combox_type()
        {
            Option option = new Option("xiangmuke", "项目卡");
            this.type_combox.Items.Add(option);
            option = new Option("huiyuanka", "会员卡");
            this.type_combox.Items.Add(option);
            this.type_combox.DisplayMember = "Value";
            this.type_combox.ValueMember = "Key";
            this.type_combox.SelectedIndex = 0; //默认选择项目卡
        }


        //卡管理加载
        private void Card_Load(object sender, EventArgs e)
        {
            show_cart_dgv();
        }

        //显示卡列表
        private void show_cart_dgv()
        {
            try
            {
                string query = string.Format("select cart_id ID,name 名称,type 类型,temp 折扣次数,price 价格 from cart where cart_id!=1 order by type");
                DataSet ds = new DataSet();
                ds = CommonDB.Query(query);
                for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                {
                    if ("xiangmuka".Equals(ds.Tables[0].Rows[index][2].ToString()))
                    {
                        //如果为项目卡
                        ds.Tables[0].Rows[index][2] = "项目卡";
                    }
                    else
                    {
                        //如果为会员卡
                        ds.Tables[0].Rows[index][2] = "会员卡";
                    }
                
                }
                this.cart_dgv.DataSource = ds.Tables[0].DefaultView;
                this.cart_dgv.Columns[0].Visible = false;
                ds.Dispose();
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //双击显示卡的信息
        private void cart_dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string name;//卡名
            string type;//卡类型
            string temp;//折扣或者次数
            string price;//价格
            
            if (e.RowIndex > -1)
            {
                cart_id = this.cart_dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
                name = this.cart_dgv.Rows[e.RowIndex].Cells[1].Value.ToString();
                type = this.cart_dgv.Rows[e.RowIndex].Cells[2].Value.ToString();
                temp = this.cart_dgv.Rows[e.RowIndex].Cells[3].Value.ToString();
                price = this.cart_dgv.Rows[e.RowIndex].Cells[4].Value.ToString();
                name_text.Text = name;
                type_combox.Text = type;
        
                if (type.Equals("项目卡"))
                {
                    serverTime_text.Text = temp;
                    discount_text.Text = "1";
                }
                else {
                    serverTime_text.Text = "0";
                    discount_text.Text = temp;
                }
                price_text.Text = price;
            }
        }

        //修改
        private void update_Click(object sender, EventArgs e)
        {
            if ("".Equals(cart_id))
            {
                MessageBox.Show("请选择要修改的卡", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            
            if (string.Empty == name_text.Text)
            {
                MessageBox.Show("名称不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.Empty == serverTime_text.Text)
            {
                MessageBox.Show("服务次数不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!isInt(serverTime_text.Text.ToString()))
            {
                MessageBox.Show("服务次数应为整数", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.Empty == discount_text.Text)
            {
                MessageBox.Show("折扣不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!isFloat(discount_text.Text.ToString()))
            {
                MessageBox.Show("折扣应为正确的数据格式", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (string.Empty == price_text.Text)
            {
                MessageBox.Show("价格不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!isFloat(price_text.Text.ToString()))
            {
                MessageBox.Show("价钱应为正确的数据格式", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string name;//卡名
            string type;//卡类型
            string temp;//折扣或者次数
            string price;//价格
            name = name_text.Text.ToString();
            type = this.type_combox.Text.ToString();
            if ("项目卡".Equals(type))
            {
                type = "xiangmuka";
                temp = serverTime_text.Text.ToString();
            }
            else
            {
                type = "huiyuanka";
                temp = discount_text.Text.ToString();
            }
            price = price_text.Text.ToString();
            //判断是否存在相同的卡名
            string sql = string.Format("select count(*) from cart where cart_id<>'{0}' and name='{1}'", cart_id,name);
            int row = 0;
            if (null != CommonDB.GetSingle(sql))
            {
                row = int.Parse(CommonDB.GetSingle(sql).ToString());
               
                if (row > 0)
                {
                    MessageBox.Show("已经存在此卡名称，请使用其它名称", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            //修改卡信息
            string update = string.Format("update cart set name='{0}',type='{1}',temp={2},price={3} where cart_id={4}", name, type, temp, price, cart_id);
            int i = CommonDB.ExecuteSql(update);
            if (i > 0)
            {
                show_cart_dgv();
                MessageBox.Show("修改成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cart_id = "";
                return;
            }
            else
            {
                MessageBox.Show("保存失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cart_id = "";
                return;
            }
         }

        //添加
        private void add_Click(object sender, EventArgs e)
        {
            if (string.Empty == name_text.Text)
            {
                MessageBox.Show("名称不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.Empty == serverTime_text.Text)
            {
                MessageBox.Show("服务次数不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (type_combox.Text.ToString().Equals("项目卡"))
            {

                if (!isInt(serverTime_text.Text.ToString()))
                {
                    MessageBox.Show("服务次数应为整数", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            else {
                if (string.Empty == discount_text.Text)
                {
                    MessageBox.Show("折扣不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (!isFloat(discount_text.Text.ToString()))
                {
                    MessageBox.Show("折扣应为正确的数据格式", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            if (string.Empty == price_text.Text)
            {
                MessageBox.Show("价格不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!isFloat(price_text.Text.ToString()))
            {
                MessageBox.Show("价钱应为正确的数据格式", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string name;//卡名
            string type;//卡类型
            string temp;//折扣或者次数
            string price;//价格
            name = name_text.Text.ToString().Trim();
            type = this.type_combox.Text.ToString().Trim();
            if ("项目卡".Equals(type))
            {
                type = "xiangmuka";
                temp = serverTime_text.Text.ToString().Trim() ;
            }
            else
            {
                type = "huiyuanka";
                temp = discount_text.Text.ToString().Trim();
            }
            price = price_text.Text.ToString().Trim();
            //判断是否存在此卡名称（如果存在则修改，如果不存在则添加）
            string sql = string.Format("select count(*) from cart where name='{0}'", name);
            int row = 0;
            if (null != CommonDB.GetSingle(sql))
            {
                row = int.Parse(CommonDB.GetSingle(sql).ToString());
                if (row > 0)
                {
                    MessageBox.Show("已经存在此卡名称，请使用其它名称", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    //添加卡信息
                    string insert = string.Format("insert cart(name,type,temp,price) values('{0}','{1}',{2},{3})", name, type, temp, price);
                    int i = CommonDB.ExecuteSql(insert);
                    if (i > 0)
                    {
                        show_cart_dgv();
                        MessageBox.Show("保存成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                        //添加部分代码，将数据动态加载到项目管理中，不可删除此部份
                        //取出新添加项目卡的卡id
                        String Proj_sql1 = "select cart_id from cart where type='" + type + "' and name='" + name + "'";
                        DataSet Proj_ds1 = new DataSet();
                        Proj_ds1 = CommonDB.Query(Proj_sql1);
                        int card_id = (int)Proj_ds1.Tables[0].Rows[0][0];
                        //遍历item表中所有项目，取出item_id
                        String Proj_sql2 = "select count(*) from item";
                        DataSet Proj_ds2 = new DataSet();
                        Proj_ds2 = CommonDB.Query(Proj_sql2);
                        int Proj_count1 = (int)Proj_ds2.Tables[0].Rows[0][0];
                        if (Proj_count1 == 0)
                        {
                            return;
                        }
                        else
                        {
                            String Proj_sql3 = "select item_id from item";
                            DataSet Proj_ds3 = new DataSet();
                            Proj_ds3 = CommonDB.Query(Proj_sql3);
                            for (int j = 0; j < Proj_ds3.Tables[0].Rows.Count; j++)
                            {
                                String item_id = Proj_ds3.Tables[0].Rows[j][0].ToString();
                                String Proj_sql4 = "select count(*) from Re_item_cart where item_id='" + item_id + "' and card_id='" + card_id + "'";
                                DataSet Proj_ds4 = new DataSet();
                                Proj_ds4 = CommonDB.Query(Proj_sql4);
                                int Proj_count2 = (int)Proj_ds4.Tables[0].Rows[0][0];
                                if (Proj_count2 == 0)
                                {
                                    String Proj_sql5 = "insert into Re_item_cart values ('" + item_id + "','" + card_id + "',0)";
                                    CommonDB.ExecuteSql(Proj_sql5);
                                }
                                else
                                {
                                    continue;
                                }
                                Proj_ds4.Dispose();
                            }
                            Proj_ds3.Dispose();
                            name_text.Text = "";
                            serverTime_text.Text = "";
                            price_text.Text = "";

                        }
                        Proj_ds2.Dispose();
                        Proj_ds1.Dispose();
                        //不可删除此部份


                        return;
                    }
                    else
                    {
                        MessageBox.Show("保存失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
            }
        }

        //选择卡类型
        private void type_combox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (type_combox.Text.ToString().Equals("项目卡"))
            {
                discount_text.Enabled = false;
                serverTime_text.Enabled = true;
            }
            else {
                discount_text.Enabled = true;
                serverTime_text.Enabled = false;
             }
             
        }
   }  
}


